﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DACU.Tools;
using DACU.VkontakteApi.Base;
using DACU.VkontakteApi.Enums;

namespace DACU.VkontakteApi.Collections
{
	class VKFriendsCollection:List<VKUserProfile>
	{
		public VKFriendsCollection()
		{
		}

		public VKFriendsCollection(IEnumerable<VKUserProfile> collection):base(collection)
		{
		}

		public bool IsEmpty
		{
			get { return Count < 1; }
		}

		public static VKFriendsCollection ParseResponse(string response,VKResponseFormats format)
		{
			if(String.IsNullOrWhiteSpace(response))throw new ArgumentNullException("reponse");
			switch (format)
			{
				case VKResponseFormats.JSON:
					throw new NotImplementedException();
				case VKResponseFormats.XML:
					return ParseXml(XElement.Parse(response, LoadOptions.None));
				default:
					return new VKFriendsCollection();
			}
		}

		public static VKFriendsCollection ParseXml(XElement response)
		{
			if(response==null || response.IsEmpty)throw new ArgumentNullException("response");
			var users = response.Elements("user");
			if (users == null || users.Count() < 1) throw new ArgumentNullException("response");
			return users.Select(VKUserProfile.ParseXml).ToFriends();
		}
	}
}
